using System.Collections.Generic;
using UnityEngine;

public class SectorMobManager : MonoBehaviour
{
	public bool drawGizmos;

	public bool spawnWithChance;

	public List<MobSectorGroupParams> mobGroups;

	public Vector3[] sectorPoints;

	public float sectorRadius;

	public Vector3 centerPoint = Vector3.zero;

	public bool _activMapItem;

	public GameObject pointForIconMap;

	private NJGMapItem iconMapItem;

	private int _maxChance;

	public bool IsActiveIconMap
	{
		get
		{
			return iconMapItem.isActive;
		}
		set
		{
			if (iconMapItem != null)
			{
				iconMapItem.enabled = value;
				if (iconMapItem.enabled)
				{
					pointForIconMap.transform.position = centerPoint;
					iconMapItem.type = 15;
				}
				else
				{
					iconMapItem.type = 0;
				}
			}
		}
	}

	private void Start()
	{
		pointForIconMap = new GameObject("pointForMap");
		pointForIconMap.transform.parent = base.transform;
		iconMapItem = pointForIconMap.AddComponent<NJGMapItem>();
		IsActiveIconMap = false;
		CalculateMaxChance();
	}

	public void CashSectorMob()
	{
		if (base.transform.childCount > 1)
		{
			sectorPoints = new Vector3[base.transform.childCount];
			for (int i = 0; i < base.transform.childCount; i++)
			{
				if (sectorPoints[i] != Vector3.zero)
				{
					sectorPoints[i] = base.transform.GetChild(i).position;
				}
			}
		}
		foreach (MobSectorGroupParams mobGroup in mobGroups)
		{
			if (mobGroup.group != null)
			{
				mobGroup.group.CalculateMaxChance();
			}
		}
		for (int j = 0; j < sectorPoints.Length; j++)
		{
			RaycastHit hitInfo;
			if (Physics.Raycast(sectorPoints[j], Vector3.down, out hitInfo, 999999f))
			{
				sectorPoints[j] = hitInfo.point + Vector3.up;
			}
		}
		CalculateCenterPoint();
		CalculateSectorRadius();
	}

	public int GetMobGroupNumber()
	{
		if (spawnWithChance)
		{
			int num = 0;
			int num2 = 0;
			num = Random.Range(0, _maxChance);
			for (int i = 0; i < mobGroups.Count; i++)
			{
				num2 += mobGroups[i].percentChance;
				if (num2 > num)
				{
					return i;
				}
			}
		}
		return Random.Range(0, mobGroups.Count);
	}

	private void CalculateCenterPoint()
	{
		centerPoint = Vector3.zero;
		Vector3[] array = sectorPoints;
		foreach (Vector3 vector in array)
		{
			centerPoint += vector;
		}
		centerPoint /= (float)sectorPoints.Length;
	}

	private void CalculateMaxChance()
	{
		foreach (MobSectorGroupParams mobGroup in mobGroups)
		{
			_maxChance += mobGroup.percentChance;
		}
	}

	private void CalculateSectorRadius()
	{
		sectorRadius = 0f;
		Vector3[] array = sectorPoints;
		foreach (Vector3 b in array)
		{
			if (Vector3.Distance(base.transform.position, b) > sectorRadius)
			{
				sectorRadius = Vector3.Distance(centerPoint, b);
			}
		}
	}
}
